package com.neuedu.oasystem.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.BuyPlan;
import com.neuedu.oasystem.service.BuyPlanService;
import com.neuedu.oasystem.service.impl.BuyPlanServiceImpl;

public class BuyPlanView {
	private Scanner scanner;
	private BuyPlanService buyPlanService;

	public void init() {
		System.out.println("===================================");
		System.out.println("                                采购计划    ");
		System.out.println("===================================");
		System.out.println("1.查询计划                             ");
		System.out.println("2.根据计划编号查询            ");
		System.out.println("3.添加计划                            ");
		System.out.println("4.删除计划                             ");
		System.out.println("5.审核计划                          ");
		System.out.println("6.登记计划                            ");
		System.out.println("7.查询审核通过计划          ");
		System.out.println("8.查询已登记计划              ");
		System.out.println("9.返回上一级                      ");
		System.out.println("===================================");
	}
	public void work() {
		scanner = new Scanner(System.in);
		buyPlanService = new BuyPlanServiceImpl();

		boolean running = true;
		while (running) {
			init();
			System.out.println("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("=========查询所有采购计划======");
				selectBuyPlan();

				
				break;
			case "2":
				System.out.println("==========根据计划编号查询========");
				findById();
				break;
			case "3":
				System.out.println("==========添加采购计划========");
				
				boolean s = addBuyPlan();
				if (s) {
					while (s) {
						s = addBuyPlan();
						if (!s) {
							break;
						}
					}
				}
				break;
			case "4":

				System.out.println("==========删除采购计划========");
				deleteBuyPlan();
				break;
			case "5":
				System.out.println("==========审核计划 ========");

				boolean s1 = editApproval();
				if (!s1) {
					while (!s1) {
						s1 = editApproval();
						if (s1) {
							break;
						}
					}
				}
				break;
			case "6":
				
				System.out.println("==========添加登记========");

				boolean s2 = editRegister();
				if (!s2) {
					while (!s2) {
						s2 = editRegister();
						if (s2) {
							break;
						}
					}
				}
				break;
			case "7":
				System.out.println("=========审核通过！======");
				selectBuyPlanApproal1();
				break;
			case "8":
				System.out.println("=========已登记计划！======");
				selectBuyRegister();
				break;
			case "9":
				System.out.println("=========返回上一级========");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}

	}

	private boolean addBuyPlan() {
		boolean format = false;
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		scanner = new Scanner(System.in);
		Date apDate = null;
		System.out.println("请输入需要增加的申请编号：      ");
		int apNo = scanner.nextInt();
		System.out.println("请输入需要增加的申请日期：      ");
		try {
			apDate = sFormat.parse(scanner.next());
		} catch (ParseException e) {
			System.out.println("日期格式错误");
			format = true;
			return format;

		}
		System.out.println("请输入申请人姓名：      ");
		String apName = scanner.next();
		System.out.println("请输入资产编号：      ");
		int assetsNo = scanner.nextInt();
		System.out.println("请输入资产单价：      ");
		double assetsPrice = scanner.nextDouble();
		System.out.println("请输入购买数量：      ");
		int assetsNumber = scanner.nextInt();
		System.out.println("审批结果(审批通过“1”，未审批“0”：      ");
		String approval = scanner.next();

		System.out.println("登记结果（已登记“y” 未登记 “n”）：      ");
		String register = scanner.next();
		BuyPlan buyPlan = new BuyPlan(apNo, apDate, apName, assetsNo, assetsPrice, assetsNumber, 0, approval, register);
		boolean res = buyPlanService.add(buyPlan);
		if (res) {
			System.out.println("新增资产成功");
			format = false;
		} else {
			System.out.println("新增资产失败");
		}
		return format;
	}

	private void selectBuyPlan() {
		System.out.println("申请编号\t\t申请日期\t\t申请人姓名\t资产编号\t资产单价\t购买数量\t总价\t\t 审批\t登记\t");
		List<BuyPlan> list = buyPlanService.findAll();
		for (BuyPlan buyPlan : list) {
			System.out.println(buyPlan.getApNo() + "\t" + buyPlan.getApDate() + "\t" + buyPlan.getEmpName() + "\t"
					+ buyPlan.getAssetsNo() + "\t" + buyPlan.getAssetsPrice() + "\t" + buyPlan.getAssetsNumber() + "\t"
					+ buyPlan.getTotalyPrice() + "\t\t" + buyPlan.getApproval() + "\t" + buyPlan.getRegister());
		}

	}

	private void deleteBuyPlan() {
		System.out.println("请输入需要删除计划编号！");
		int buyPlanId = scanner.nextInt();
		boolean re = buyPlanService.deleteById(buyPlanId);
		if (re) {
			System.out.println("删除成功！");
		}
	}

	private void selectBuyPlanApproal1() {
		System.out.println("申请编号\t\t申请日期\t\t申请人姓名\t资产编号\t资产单价\t购买数量\t总价\t\t 审批\t登记\t");
		List<BuyPlan> list = buyPlanService.findByApproval();
		for (BuyPlan buyPlan : list) {
			System.out.println(buyPlan.getApNo() + "\t" + buyPlan.getApDate() + "\t" + buyPlan.getEmpName() + "\t"
					+ buyPlan.getAssetsNo() + "\t" + buyPlan.getAssetsPrice() + "\t" + buyPlan.getAssetsNumber() + "\t"
					+ buyPlan.getTotalyPrice() + "\t\t" + buyPlan.getApproval() + "\t" + buyPlan.getRegister());
		}
	}
	private void selectBuyRegister() {
		System.out.println("申请编号\t\t申请日期\t\t申请人姓名\t资产编号\t资产单价\t购买数量\t总价\t\t 审批\t登记\t");
		List<BuyPlan> list = buyPlanService.findByRegister();
		for (BuyPlan buyPlan : list) {
			System.out.println(buyPlan.getApNo() + "\t" + buyPlan.getApDate() + "\t" + buyPlan.getEmpName() + "\t"
					+ buyPlan.getAssetsNo() + "\t" + buyPlan.getAssetsPrice() + "\t" + buyPlan.getAssetsNumber() + "\t"
					+ buyPlan.getTotalyPrice() + "\t\t" + buyPlan.getApproval() + "\t" + buyPlan.getRegister());
		}
	}
	private void findById() {
		buyPlanService = new BuyPlanServiceImpl();
		System.out.println("请输入需要删除的计划编号");
		int id = scanner .nextInt();
		BuyPlan buyPlan =new BuyPlan();
		buyPlan = buyPlanService.findById(id);
		System.out.println("申请编号\t\t申请日期\t\t申请人姓名\t资产编号\t资产单价\t购买数量\t总价\t\t 审批\t登记\t");
		System.out.println(buyPlan.getApNo() + "\t" + buyPlan.getApDate() + "\t" + buyPlan.getEmpName() + "\t"
				+ buyPlan.getAssetsNo() + "\t" + buyPlan.getAssetsPrice() + "\t" + buyPlan.getAssetsNumber() + "\t"
				+ buyPlan.getTotalyPrice() + "\t\t" + buyPlan.getApproval() + "\t" + buyPlan.getRegister());
	}
	private boolean editApproval() {
		boolean re = false;
		buyPlanService = new BuyPlanServiceImpl();
		BuyPlan buyPlan =new BuyPlan();
		System.out.println("请输入需要审批的计划编号:");
		int i = scanner.nextInt();
		System.out.println("同意：1    不同意：0");
		String agreeString = scanner.next();
		buyPlan.setApNo(i);
		buyPlan.setApproval(agreeString);
		String agreString="1";
		String aString = "0";
		if (buyPlan.getApproval().equals(agreString)||buyPlan.getApproval().equals(aString)) {
			boolean de = false;
			de = buyPlanService.editApproval(buyPlan);
			if (de) {
				System.out.println("审批成功");
			}else {
				System.out.println("审批失败");
			}
			re =true;
		}else {
			re = false;
		}
		
		
		
		return re;
	}
	private boolean editRegister() {
		boolean re = false;
		buyPlanService = new BuyPlanServiceImpl();
		BuyPlan buyPlan =new BuyPlan();
		System.out.println("请输入需要登记的计划编号:");
		int i = scanner.nextInt();
		System.out.println("登记：y    不登记：n");
		String agreeString = scanner.next();
		buyPlan.setApNo(i);
		buyPlan.setRegister(agreeString.toLowerCase());
		String agreString="y";
		String aString = "n";
		if (buyPlan.getRegister().equals(agreString)||buyPlan.getRegister().equals(aString)) {
			boolean de = false;
			de = buyPlanService.editRegister(buyPlan);
			if (de) {
				System.out.println("登记成功");
			}else {
				System.out.println("登记失败");
			}
			re =true;
		}else {
			re = false;
		}
		
		return re;
	}
}